# Create your views here.
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.contrib.auth import logout
from django.template import RequestContext
from django.db import transaction
from market.authentification import authentificatePlayer, authentificateUser, authentificateUserWithBetroom
from market.models import UserProfile
from market.models import BetRoom, Strategy, AssetPercentageInStrategy, Fund
from market.models import Asset, Player
from market.strategyAndFundHelper import getFundMtM, getPortfolioMtM

from market.stupidUserException import StupidUserException
from market.exceptionHandler import handleRequestPlayerPOSTWithExceptionWhenNotSuspended
from market.exceptionHandler import handleRequestBetroomAdminPOSTWithException


import os, os.path
from market.baseCreator import constructRoomFromFiles
from market.baseSaver import saveRoomToFiles



################################################################################################################
# web to create a new betroom
def createBetroom1(request):
        
        alternative_response_list = [] #the answer to return if the player could not be authentificated with a dummy initialisation
        if not( authentificateUser( request, alternative_response_list ) ):
                return alternative_response_list[0]
        
        betroom = BetRoom.objects.filter( personInCharge = request.user, status__lte = 0 )
        if len( betroom ) == 0:
                return render_to_response('CreateBetroom/createBetroom1.html',
                                        {'userName':request.user.username, 'message':'' },
                                        context_instance=RequestContext(request) )
        if len( betroom ) == 1:
		request.user.get_profile().nameOfTheLastVistedBetRoom = betroom[0].name
		request.user.get_profile().save()
                return HttpResponseRedirect("/createBetroom/step2/")

        raise Exception('There should be just one betroom per user being launched at once.')
                


################################################################################################################
# web to create a new betroom
def createBetroom2(request):
        
        alternative_response_list = [] #the answer to return if the player could not be authentificated with a dummy initialisation
        if not( authentificateUser( request, alternative_response_list ) ):
                return alternative_response_list[0]
        
        betroom = BetRoom.objects.filter( personInCharge = request.user, status__lte = 0 )
        if len( betroom ) == 0:
                return HttpResponseRedirect("/createBetroom/step1/")
        
        if len( betroom ) != 1:
                raise Exception('There should be one bet room beeing launched.')
        
        if betroom[0].roomType == 0:
                return render_to_response('CreateBetroom/createBetroom2.html',
                        {'userName':request.user.username, 'betroom':betroom[0] },
                        context_instance=RequestContext(request) )
        else:
                return render_to_response('CreateBetroom/createBetroom5.html',
                        {'userName':request.user.username, 'betroom':betroom[0] },
                        context_instance=RequestContext(request) )


################################################################################################################
# web to create a new betroom
def createBetroom3(request):
        
        alternative_response_list = [] #the answer to return if the player could not be authentificated with a dummy initialisation
        if not( authentificateUser( request, alternative_response_list ) ):
                return alternative_response_list[0]
        
        betroom = BetRoom.objects.filter( personInCharge = request.user, status__lte = 0 )
        if len( betroom ) == 0:
                return HttpResponseRedirect("/createBetroom/step1/")
        
        if len( betroom ) != 1:
                raise Exception('There should be one bet room beeing launched.')
        
        return render_to_response('CreateBetroom/createBetroom3.html',
                        {'userName':request.user.username, 'betroom':betroom[0] },
                        context_instance=RequestContext(request) )

################################################################################################################
# web to create a new betroom
def createBetroom4(request):
        
        alternative_response_list = [] #the answer to return if the player could not be authentificated with a dummy initialisation
        if not( authentificateUser( request, alternative_response_list ) ):
                return alternative_response_list[0]
        
        betroom = BetRoom.objects.filter( personInCharge = request.user, status__lte = 0 )
        if len( betroom ) == 0:
                return HttpResponseRedirect("/createBetroom/step1/")
        
        if len( betroom ) != 1:
                raise Exception('There should be one bet room beeing launched.')
        
        return render_to_response('CreateBetroom/createBetroom4.html',
                        {'userName':request.user.username,
                         'betroom':betroom[0],
                         'onlyAdmin':( len( Player.objects.filter( mainStrategy__betroom = betroom[0], userBeingThisPlayer = request.user ))==0)
                         },
                        context_instance=RequestContext(request) )

################################################################################################################
# web to create a new betroom
def createBetroom5(request):
        
        alternative_response_list = [] #the answer to return if the player could not be authentificated with a dummy initialisation
        if not( authentificateUser( request, alternative_response_list ) ):
                return alternative_response_list[0]
        
        betroom = BetRoom.objects.filter( personInCharge = request.user, status__lte = 0 )
        if len( betroom ) == 0:
                return HttpResponseRedirect("/createBetroom/step1/")
        
        if len( betroom ) != 1:
                raise Exception('There should be one bet room beeing launched.')
        
        return render_to_response('CreateBetroom/createBetroom5.html',
                        {'userName':request.user.username, 'betroom':betroom[0] },
                        context_instance=RequestContext(request) )

################################################################################################################
# web to create a new betroom
def enterBetroom1(request):
        
        alternative_response_list = [] #the answer to return if the player could not be authentificated with a dummy initialisation
        if not( authentificateUser( request, alternative_response_list ) ):
                return alternative_response_list[0]

        try:
                betroom = BetRoom.objects.get( name = request.user.get_profile().nameOfTheLastVistedBetRoom )
        except:
                return HttpResponseRedirect("/betrooms/")

        players = Player.objects.filter( mainStrategy__betroom = betroom, userBeingThisPlayer = request.user )
        if len( players ) > 1:
                raise Exception('There should be no more than one player with this name in this betroom')

        if betroom.personInCharge == request.user:
                return HttpResponseRedirect("/createBetroom/step4/")
                
        if len( players ) == 1:
                return HttpResponseRedirect("/enterBetroom/step2/")
        
        return render_to_response('CreateBetroom/enterBetroom1.html',
                        {'userName':request.user.username,
                         'betroom':betroom
                         },
                        context_instance=RequestContext(request) )

################################################################################################################
# web to create a new betroom
def enterBetroom2(request):
        
        betroom = []
        alternative_response_list = [] #the answer to return if the player could not be authentificated with a dummy initialisation
        if not( authentificateUserWithBetroom( request, betroom, alternative_response_list ) ):
                return alternative_response_list[0]
        
        if betroom[0].roomType == 0:
                return render_to_response('CreateBetroom/enterBetroom2.html',
                        {'userName':request.user.username, 'betroom':betroom[0] },
                        context_instance=RequestContext(request) )
        else:
                return render_to_response('CreateBetroom/enterBetroom2Contract.html',
                        {'userName':request.user.username, 'betroom':betroom[0] },
                        context_instance=RequestContext(request) )


                

